package com.andy.nature.view

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.andy.nature.R
import com.andy.nature.databinding.TabBottomItemBinding

/**
 * Author:Andy
 * Date:2023/4/4 15:46
 * Description:TabView
 */
class TabView : ConstraintLayout {
    private lateinit var mBinding: TabBottomItemBinding
    private val mTabHomeIds = intArrayOf(R.drawable.ic_tab_home_white, R.drawable.ic_tab_home_black)
    private val mTabGlobalIds = intArrayOf(R.drawable.ic_tab_global_white, R.drawable.ic_tab_global_black)
    private val mTabMsgIds = intArrayOf(R.drawable.ic_tab_msg_white, R.drawable.ic_tab_msg_black)
    private val mTabPersonIds = intArrayOf(R.drawable.ic_tab_person_white, R.drawable.ic_tab_person_black)
    private val tabMap = mapOf(0 to mTabHomeIds, 1 to mTabGlobalIds, 2 to mTabMsgIds, 3 to mTabPersonIds)


    constructor(context: Context) : super(context) {
        TabView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        mBinding = TabBottomItemBinding.inflate(LayoutInflater.from(context), this)
    }

    fun setSelect(position: Int) {
        tabMap[position]?.let {
            mBinding.ivTab.setImageResource(it[1])
            selectAnimation()
        }
    }

    fun setUnselect(position: Int) {
        tabMap[position]?.let {
            mBinding.ivTab.setImageResource(it[0])
        }
    }

    fun setBadge(count: Int) {
        mBinding.apply {
            tvBadge.isVisible = count > 0
            if (count > 99) {
                mBinding.tvBadge.text = "99+"
            } else {
                mBinding.tvBadge.text = "$count"
            }
        }
    }

    fun clearBadge() {
        mBinding.apply {
            tvBadge.isVisible = false
            mBinding.tvBadge.text = ""
        }
    }


    private fun selectAnimation() {
        mBinding.apply {
            val scaleX1 = ObjectAnimator.ofFloat(ivTab, "scaleX", 0.75f, 1.05f)
            val scaleY1 = ObjectAnimator.ofFloat(ivTab, "scaleY", 0.75f, 1.05f)
            val animSet1 = AnimatorSet()
            animSet1.duration = 100
            animSet1.interpolator = DecelerateInterpolator(1.3f)
            animSet1.playTogether(scaleX1, scaleY1)
            animSet1.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                }

                override fun onAnimationEnd(animation: Animator) {
                    val scaleX2 = ObjectAnimator.ofFloat(ivTab, "scaleX", 1.05f, 1.0f)
                    val scaleY2 = ObjectAnimator.ofFloat(ivTab, "scaleY", 1.05f, 1.0f)
                    val animSet2 = AnimatorSet()
                    animSet2.duration = 100
                    animSet2.interpolator = AccelerateDecelerateInterpolator()
                    animSet2.playTogether(scaleX2, scaleY2)
                    animSet2.start()
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }

            })
            animSet1.start()
        }
    }

}